package com.hd.trans.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

import com.hd.trans.db.greenDao.AiChatRecordDao;
import com.hd.trans.db.greenDao.DaoMaster;
import com.hd.trans.db.greenDao.FileTranslateRecordDao;
import com.hd.trans.db.greenDao.HuDunLanguageDao;
import com.hd.trans.db.greenDao.OCRImageHistoryDao;
import com.hd.trans.db.greenDao.TranslateRecordDao;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.database.Database;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yang on 2017/10/12.
 * 数据库升级
 */
public class MyOpenHelper extends DaoMaster.OpenHelper {

    private List<Class<? extends AbstractDao<?, ?>>> upgradedClasses = new ArrayList<>();//已经升级过的表格

    public MyOpenHelper(Context context, String name) {
        super(context, name);
    }

    public MyOpenHelper(Context context, String name, CursorFactory factory) {
        super(context, name, factory);
    }

    /**
     * 数据库升级
     *
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {

        Log.i("onUpgrade","===================database onUpgrade. oldVersion is " + oldVersion + " , newVersion is " + newVersion);

        //操作数据库的更新 有几个表升级都可以传入到下面
        if (oldVersion <= 1) {
            /** 版本2翻译记录表新增字段(是否为逆向 is_reverse, 翻译状态 record_state) 新增表 OCRImageHistory*/
            MigrationHelper.getInstance().migrate(db, TranslateRecordDao.class, OCRImageHistoryDao.class);
            upgradedClasses.add(TranslateRecordDao.class);
            upgradedClasses.add(OCRImageHistoryDao.class);
        }
        if(oldVersion <= 2) {
            if(!upgradedClasses.contains(HuDunLanguageDao.class)){
                /**版本3:HuDunLanguageDao添加字段support_discern*/
                MigrationHelper.getInstance().migrate(db, HuDunLanguageDao.class);
                upgradedClasses.add(HuDunLanguageDao.class);
            }
        }
        if(oldVersion <= 3) {
            if(!upgradedClasses.contains(FileTranslateRecordDao.class)){
                /**版本4添加了表格FileTranslateRecord*/
                MigrationHelper.getInstance().migrate(db, FileTranslateRecordDao.class);
                upgradedClasses.add(FileTranslateRecordDao.class);
            }
        }
        if(oldVersion <= 4) {
            if(!upgradedClasses.contains(FileTranslateRecordDao.class)){
                /**版本5表格FileTranslateRecord添加字段file_name*/
                MigrationHelper.getInstance().migrate(db, FileTranslateRecordDao.class);
                upgradedClasses.add(FileTranslateRecordDao.class);
            }
        }
        if(oldVersion <= 5) {
            if(!upgradedClasses.contains(HuDunLanguageDao.class)){
                /**版本6表格HuDunLanguage添加字段file_recognition_code*/
                MigrationHelper.getInstance().migrate(db, HuDunLanguageDao.class);
                upgradedClasses.add(HuDunLanguageDao.class);
            }
        }
        if(oldVersion <= 6) {
            if(!upgradedClasses.contains(HuDunLanguageDao.class)){
                /**版本7表格HuDunLanguage添加字段ali_app_key*/
                MigrationHelper.getInstance().migrate(db, HuDunLanguageDao.class);
                upgradedClasses.add(HuDunLanguageDao.class);
            }
        }
        if(oldVersion <= 7) {
            if(!upgradedClasses.contains(AiChatRecordDao.class)){
                /**版本8添加表格AiChatRecord*/
                MigrationHelper.getInstance().migrate(db, AiChatRecordDao.class);
                upgradedClasses.add(AiChatRecordDao.class);
            }
        }
        if(oldVersion <= 8) {
            if(!upgradedClasses.contains(TranslateRecordDao.class)){
                /**版本9添加字段pinyin_org, pinyin_translate, record_type, voice_file*/
                MigrationHelper.getInstance().migrate(db, TranslateRecordDao.class, FileTranslateRecordDao.class);
                upgradedClasses.add(TranslateRecordDao.class);
                upgradedClasses.add(FileTranslateRecordDao.class);
            }
        }
    }


}
